package com.yonyou.pmclouds.measureindex.processor;

import com.yonyou.pmclouds.basecom.bp.processor.MultiProcessor;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.measureindex.entity.MeasureIndexVO;
import com.yonyou.pmclouds.measureindex.mapper.MeasureIndexMapper;

import java.util.HashMap;
import java.util.HashSet;

public class MultiAddNameAndCodeCheck implements MultiProcessor<MeasureIndexVO> {

    private MeasureIndexMapper mapper;
    public MultiAddNameAndCodeCheck(MeasureIndexMapper mapper){
        this.mapper = mapper;
    }
    @Override
    public void process(MeasureIndexVO[] vos, MeasureIndexVO[] origs) throws BusinessException {

        String tenantId = RuntimeEnvironment.getTenantId();
        HashSet<String> codeSet = new HashSet<>();
        HashSet<String> nameSet = new HashSet<>();

        for (MeasureIndexVO vo : vos) {
            String code = vo.getCode();
            String name = vo.getName();
            if(mapper.countCode(code,tenantId) != 0 || codeSet.contains(code)){
                throw new BusinessException("已经存在相同的编码:"+code+"请修改!");
            }
            if(mapper.countName(name,tenantId) != 0 || nameSet.contains(name)){
                throw new BusinessException("已经存在相同的名称:"+name+"请修改!");
            }
            codeSet.add(code);
            nameSet.add(name);
        }


    }
}
